goog.provide('dkpoints.raid.parser');

goog.require('dkpoints.raid');

/**
 * Function that takes a Headcount XML string and returns a raid object.
 * @param {string} raidXML The Headcount exported XML string
 * @return {!dkpoints.raid.Raid} The object representation of the raid
 */
dkpoints.raid.parser.buildFromHeadcount = function(raidXML) {
  var me = dkpoints.raid.parser;
  
  var msgGroup = "Raid Parser";
  var msgID = "parse_status";
  
  // Create a new Raid object.
  // Set up references to its attributes, because they're gonna be called a lot.
  var raidOb = new dkpoints.raid.Raid();
    var rInfo = raidOb["info"];
    var rPlayers = raidOb["players"];
    var rLoot = raidOb["loot"];
  
  var parser = new DOMParser();
  var raidDoc = parser.parseFromString(raidXML,"text/xml");
  
  // Get the date of the raid from the <end> element.
  rInfo["date"] = raidDoc.getElementsByTagName("raid")[0].getElementsByTagName("end")[0].firstChild.nodeValue;
  
  // Construct the name of the raid by the bosses killed.
  var raidName = "";
  var bossesKilled = raidDoc.getElementsByTagName("bossKills")[0].getElementsByTagName("boss");
  for(var i=0; i < bossesKilled.length; i++) {
    raidName += bossesKilled[i].getElementsByTagName("name")[0].firstChild.nodeValue + ", ";
  }
  
  // Remove the trailing ", " from the loop
  raidName = raidName.substring(0, raidName.length - 2);
  rInfo["name"] = raidName;
  
  // Get the zone name.
  rInfo["zone"] = raidDoc.getElementsByTagName("zone")[0].childNodes[0].nodeValue;
  
  // Build the array of player objects.
  var raidMembers = raidDoc.getElementsByTagName("players")[0].getElementsByTagName("player");
  for(var i = 0; i < raidMembers.length; i++) {
    var thisGuy = {};
    thisGuy["playerName"] = raidMembers[i].getElementsByTagName("name")[0].firstChild.nodeValue,
    thisGuy["playerClass"] = raidMembers[i].getElementsByTagName("class")[0].firstChild.nodeValue.toLowerCase()
    
    if(thisGuy["playerClass"] == "death knight")
    {  thisGuy["playerClass"] = "deathknight";  }
    
    rPlayers.push(thisGuy);
  }
  
  var iconRE = /Interface\\Icons\\(\w*)/;
  
  // Build the array of loot objects.
  var lootElements = raidDoc.getElementsByTagName("item");
  for(var j = 0; j < lootElements.length; ++j) {
    var lootEl = lootElements[j];
    var thisThing = {};
    thisThing["item"] = lootEl.getElementsByTagName("id")[0].firstChild.nodeValue;
    thisThing["name"] = lootEl.getElementsByTagName("name")[0].firstChild.nodeValue;
    
    var looter = thisThing["looter"] = lootEl.getElementsByTagName("looter")[0].firstChild.nodeValue;
    if(looter == "Banked" || looter == "Disenchanted") {
      thisThing["interest"] = looter;
    }
    else {
      thisThing["interest"] = "Onspec";
    }
    
    // Headcount uses a numerical index to represent quality.  4 = Epic, 3 = Superior, etc.
    thisThing["quality"] = dkpoints.utils.IQuality[lootEl.getElementsByTagName("rarity")[0].firstChild.nodeValue];
    thisThing["icon"] = iconRE.exec(lootEl.getElementsByTagName("texture")[0].firstChild.nodeValue)[1];
    
    rLoot.push(thisThing);
  }
  
  // status.updateMessage("Parse completed in " + (Date.now() - parseStart) + " milliseconds.", msgID, msgGroup, 10000);
  rInfo["_parseOrigin"] = "Headcount";
  rInfo["dirty"] = true; // A new raid is always dirty.
  return raidOb;
};

/**
 * Function that takes a new-line separated list of player names and returns a raid object.
 * @param {string} playerList The list of players.
 * @return {!dkpoints.raid.Raid} The object representation of the raid
 */
dkpoints.raid.parser.buildFromPlayerList = function(playerList) {
  var raidOb = new dkpoints.raid.Raid();
  
  var rPlayers = raidOb["players"];
  var playerArr = playerList.split("\n");
  for(var i = 0; i < playerArr.length; ++i) {
    var thisGuy = {};
    thisGuy["playerName"] = playerArr[i];
    thisGuy["playerClass"] = "noclass";
    
    rPlayers[i] = thisGuy;
  }
  
  raidOb["info"]["_parseOrigin"] = "PlayerList";
  rInfo["dirty"] = true; // A new raid is always dirty.
  return raidOb;
};